<template>
  <div class="home-container">
    <van-nav-bar title="搜索" class="page-nav-bar" fixed>
      <van-button
        round
        type="info"
        class="search-btn"
        slot="title"
        size="small"
        icon="search"
        @click="search()"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 标签栏 -->
    <van-tabs v-model="active" animated swipeable class="channel-tabs">
      <!-- 渲染个人频道数据 -->
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <!-- 列表组件 -->
        <article-list :channel="item"></article-list>
      </van-tab>
      <!-- 汉堡包 -->
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <!-- 频道内容子组件 -->
      <channel-edit
        :myChannels="channels"
        :active="active"
        @update-active="onUpdateActive($event)"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
// 导入列表子组件
import ArticleList from './components/article-list.vue'
// 导入频道子组件
import ChannelEdit from './components/channel-edit.vue'
// 导入vuex中的user
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  },
  data () {
    return {
      active: 0, // 切换
      channels: [], // 个人频道
      isChannelEditShow: false
    }
  },
  async created () {
    // 获取用户频道
    try {
      let channels = []
      if (this.user) {
        // 已登录获取接口数据
        const { data } = await getUserChannels()
        channels = data.data.channels
      } else {
        // 未登录 判断是否是本地存数数据
        const localChannels = getItem('TOUTIAO_CHANNELS')
        if (localChannels) {
          console.log(22)
          // 有本地数据 就直接添加进数组中使用
          channels = localChannels
          console.log(channels)
        } else {
          console.log(33)
          // 没有就请求获取 默认频道列表
          const { data } = await getUserChannels()
          channels = data.data.channels
        }
      }
      this.channels = channels
    } catch (e) {
      this.$toast('获取频道列表数据失败')
    }
  },
  methods: {
    onUpdateActive (e, isChannelEditShow = true) {
      this.active = e
      this.isChannelEditShow = !isChannelEditShow
    },
    search () {
      this.$router.push('/search')
    }
  }

}
</script>

<style lang="less" scoped>
.home-container{
  padding-top: 174px;
  padding-bottom: 100px;
    /deep/.van-nav-bar__title {
    max-width: unset;
  }
}
.search-btn {
  width: 555px;
  height: 64px;
  background-color: #5babfb;
  border: none;
  font-size: 28px;
  .van-icon {
    font-size: 32px;
  }
}
/deep/ .channel-tabs {
   .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }  .home-container{
  padding-top: 174px;
  padding-bottom: 100px;
}

  .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
    height: 80px;
    // flex-shrink: 0 !important;
    // flex-basis: auto;
    .van-tab__text {
      font-size: 27px;
      color: #777777;
    }
  }
  .van-tab--active .van-tab__text {
    color: #333 !important;
  }
  .van-tabs__line {
    width: 31px !important;
    height: 6px;
    background: rgba(50, 150, 250, 1);
    border-radius: 3px;
    bottom: 8px;
  }
  .van-tabs__nav {
    padding: 0;
  }
}.placeholder {
  flex-shrink: 0;
  width: 66px;
  height: 82px;
}
.hamburger-btn {
  position: fixed;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 82px;
  background-color: #fff;
  opacity: 0.902;
  i.toutiao {
    font-size: 33px;
  }
  &:before {
    content: "";
    position: absolute;
    left: 0;
    width: 1px;
    height: 100%;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
}

</style>
